package osp.Memory;
/**
    The PageTable class represents the page table for a given task.
    A PageTable consists of an array of PageTableEntry objects.  This
    page table is of the non-inverted type.

    @OSPProject Memory
 */
import java.lang.Math;
import java.util.ArrayList;
import java.util.List;

import osp.Tasks.*;
import osp.Utilities.*;
import osp.IFLModules.*;
import osp.Hardware.*;

public class PageTable extends IflPageTable
{
	/** 
	The page table constructor. Must call

	    super(ownerTask)

	as its first statement.

	@OSPProject Memory
	 */
	public PageTable(TaskCB ownerTask)
	{
		super(ownerTask);

		int tamPagina = (int) Math.pow(2, MMU.getPageAddressBits());

		pages = new PageTableEntry[tamPagina];

		for(int i = 0; i < pages.length; i++)
		{
			pages[i] = new PageTableEntry(this, i);
		}
	}

	/**
       Frees up main memory occupied by the task.
       Then unreserves the freed pages, if necessary.

       @OSPProject Memory
	 */
	public void do_deallocateMemory()
	{
		TaskCB task = this.getTask();
		FrameTableEntry fte;

		for(int i = 0; i < pages.length; i++)
		{
			fte = pages[i].getFrame(); 
			if(fte!=null)
			{
				fte.setPage(null);
				fte.setDirty(false);
				fte.setReferenced(false);
				if (fte.getReserved()!=null && fte.getReserved().getID() == task.getID())
				{
					fte.setUnreserved(task);
				}
			}
		}
	}
}
